﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;
namespace IHISCETeim.Web.MachineSys.FTradeModel
{
    public partial class FTradeReceivablesHuanJi : PageBase
    {
        protected override string DWZRef
        {
            get
            {
                return "FTradeReceivablesListEasyUI";
            }
        }

        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.应收帐款_操作 };
            }
        }

        protected int FTradeID
        {
            get
            {

                return GetRequestValue<int>("FTradeID", 0);
            }
        }
        private FTradeReceivablesInfo _objFx = null;

        protected FTradeReceivablesInfo obj
        {
            get
            {
                if (_objFx == null)
                    _objFx = FTradeReceivablesController.GetByFTradeID(FTradeID);
                return _objFx;
            }
        }
        #region 页面方法
        /// <summary>
        /// 获取所有销售类型SELECT　项
        /// </summary>
        /// <param name="XiaoShouLeiXing"></param>
        /// <returns></returns>
        protected string GetXiaoShouLeiXing(string XiaoShouLeiXing)
        {
            List<FTradeSaleTypeConfigInfo> list = FTradeSaleTypeConfigController.GetAll();
            list.Add(new FTradeSaleTypeConfigInfo(){ XiaoShouLeiXing="请选择"});
            StringBuilder sb = new StringBuilder();
            foreach (FTradeSaleTypeConfigInfo itm in list)
            {
                sb.AppendFormat("<option value=\"{0}\" {1}>{0}</option>", itm.XiaoShouLeiXing, itm.XiaoShouLeiXing == XiaoShouLeiXing ? "selected" : "");
            }

            return sb.ToString();
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                addmenu();
                
            }        
        }
        private void addmenu()
        {
            //负记录信息
            FTradeReceivablesInfo objNewFu = Request.SerializableObject<FTradeReceivablesInfo>();
            objNewFu.HeTongHao = GetRequestValue<string>("HeTongHao_Fu", "");
            objNewFu.XiaoShouShiJian = GetRequestValue<DateTime>("XiaoShouShiJian_Fu", DateTime.Now);
            objNewFu.JingXiaoShang = GetRequestValue<string>("JingXiaoShang_Fu", "");
            objNewFu.YingYeJingLi = GetRequestValue<string>("YingYeJingLi_Fu", "");
            objNewFu.XingHao = GetRequestValue<string>("XingHao_Fu", "");
            objNewFu.JiHao = GetRequestValue<string>("JiHao_Fu", "");
            objNewFu.Price = GetRequestValue<decimal>("Price_Fu", 0);
            objNewFu.GPSFeiYong = GetRequestValue<decimal>("GPSFeiYong_Fu",0);
            objNewFu.YueCuXiaoJinE = GetRequestValue<decimal>("YueCuXiaoJinE_Fu", 0);
            objNewFu.HeTongJinE = GetRequestValue<decimal>("HeTongJinE_Fu", 0);
            objNewFu.FuKuanTiaoKuan = GetRequestValue<string>("FuKuanTiaoKuan_Fu", "");
            //新机信息
            FTradeReceivablesInfo objNew = Request.SerializableObject<FTradeReceivablesInfo>();
            //objNew.YueCuXiaoJinE = 0 - objNew.YueCuXiaoJinE;

            FTradeSalesTypeInfo objSalesType = Request.SerializableObject<FTradeSalesTypeInfo>();
            objSalesType.State = "正常";
            objSalesType.RealName = base.User.RealName;
            objSalesType.ExConfig = GetRequestValue<string>("SaleTypeConfig_Type", ""); //GetXiaoShouLeiXingExConfig(objSalesType.XiaoShouLeiXing);

            List<FTradeAgeDetailInfo> listAgetDetails = new List<FTradeAgeDetailInfo>();

            char[] caSplit = new char[] { ',' };
            string[] Page_JinEs = GetRequestValue<string>("Page_JinE", "").Split(caSplit);
            string[] Page_DaoQiRis = GetRequestValue<string>("Page_DaoQiRi", "").Split(caSplit);
            string[] Page_ShowIndexs = GetRequestValue<string>("Page_ShowIndex", "").Split(caSplit);
            string[] Page_TongJiNames = GetRequestValue<string>("Page_TongJiName", "").Split(caSplit);
            string[] Page_ShowTitles = GetRequestValue<string>("Page_ShowTitle", "").Split(caSplit);
            string[] Page_SuspendedMarks = GetRequestValue<string>("Page_SuspendedMark", "").Split(caSplit);

            for (int index = 0; index < Page_JinEs.Count(); index++)
            {
                if (string.IsNullOrEmpty(Page_JinEs[index]) || Page_JinEs[index] == "0")
                    continue;

                FTradeAgeDetailInfo objx = new FTradeAgeDetailInfo();
                objx.ShowIndex = index;// Convert.ToInt32(Page_ShowIndexs[index]);
                objx.TongJiName = Page_TongJiNames[index];
                objx.ShowTitle = Page_ShowTitles[index];
                objx.JinE = Convert.ToDecimal(Page_JinEs[index]);
                objx.DaoQiRi = Convert.ToDateTime(Page_DaoQiRis[index]);
                objx.SuspendedMark = 0;
                objx.RealName = base.User.RealName;
                objx.YearRate = 0;
                objx.TanQeChenBen = 0;
                listAgetDetails.Add(objx);
                if (objNew.XiaoShouLeiXing == "全款" || objNew.XiaoShouLeiXing == "2个月帐期" || objNew.XiaoShouLeiXing == "3个月帐期" || objNew.XiaoShouLeiXing == "4个月及以上帐期")
                {
                    objNew.DaoQiRi = objx.DaoQiRi.ToString("yyyy-MM-dd");
                }
                else
                    objNew.DaoQiRi = "";
            }



            int FCatheringNum = FTradeReceivablesController.AddHuanJi(objNewFu, objNew, objSalesType, listAgetDetails);

            if (FCatheringNum > 0)
            {
                string msg2 = "已成功换机,请转到[收款及冲帐]界面,对调帐金额进行处理.";
                string tabTitle = "收款及冲帐";
                string taburl = "FTradeModel/FTradeCatheringListEasyUI.aspx";
                string tabrelId = "FTradeCatheringListEasyUI";
                SuccessOpenNavTab(msg2, tabTitle, taburl, tabrelId, true);
            }

            if (FCatheringNum == 0)
            {
                SuccessJson("已换机成功", this.DWZRef);
            }
            if (FCatheringNum < 0)
            {
                ErrorJson("换机失败");
            }


        }

        private string GetXiaoShouLeiXingExConfig(string XiaoShouLeiXing)
        {
            List<string> PageConfig = new List<string>();
            PageConfig.Add("PageConfig_QiShu");
            PageConfig.Add("PageConfig_QiShuType");
            PageConfig.Add("PageConfig_JinE");
            StringBuilder sb = new StringBuilder();
            switch (XiaoShouLeiXing.Trim())
            {
                case "全款":
                case "首付代垫":
                case "置换分期":
                    return "";
                    break;
                case "短期分期":
                case "置换加分期":
                    foreach (string itm in PageConfig)
                    {
                        sb.AppendFormat("{0}|{1}", itm, GetRequestValue<string>(itm, ""));
                    }
                    return sb.ToString();
                    break;
                default:
                    return "";
                    break;
            }

            return "";
        }
    }
}